<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Sanpham.php';
$sanphamOb = new Sanpham();
?>
<?php include('layout/header.php') ?>
<div id="templatmeo_content1">
    <h2><a name="sp">Sản phẩm khuyến mãi</a></h2>

    <?php
    include_once 'lib/dataprovider.php';
    if (!isset($_GET['page'])) {
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
    $max_results = 9;
    $from = (($page * $max_results) - $max_results);
    $sql = DataProvider::ExecuteQuery("select * from khuyenmai order by ngaycapnhat desc LIMIT $from, $max_results");
    while ($sp = DataProvider::ExecuteQueryArray($sql)) {
        ?>
        <div class="content_section">
            <div class="product_box">
                <h3 id="ten_sp_<?php echo $sp['id'] ?>"><?php echo $sp['ten'] ?></h3>
                <div class="image_wrapper" ><a href="chitietsanphamKM.php?id=<?php echo $sp['id'] ?>" target="_parent"><img class="img" src="<?php echo $sp['hinhanh'] ?>" /></a> </div>
                <p class="price">Giá chỉ: <?php echo number_format($sp['giaKM']) ?> VNĐ</p>
                <p class="price"><strike>Giá gốc: <?php echo number_format($sp['gia']) ?> VNĐ</strike></p>
                <a href="chitietsanphamKM.php?id=<?php echo $sp['id'] ?>">Chi tiết</a> | <a class="them-gio-hang" rel="<?php echo $sp['id'] ?>" href="javascript:void(0)">Chọn</a>
            </div>
        </div> <!-- end of templatmeo_content -->

        <?php
    }
    ?>
    <div style="float: right; margin: 0px 325px 0px"><?php
    $total_results = DataProvider::TongSanPham("SELECT COUNT(*) FROM khuyenmai");
    $total_pages = ceil($total_results / $max_results);
    echo "<center>Trang<br />";
    if ($page > 1) {
        $prev = ($page - 1);
        echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$prev\"><<Trang trước</a>&nbsp;";
    }
    for ($i = 1; $i <= $total_pages; $i++) {
        if (($page) == $i) {
            echo "<b>$i&nbsp;</b>";
        } else {
            echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$i\">$i</a>&nbsp;";
        }
    }
    ?></div>
        
</div> <!-- end of templatemo_wrapper -->
<script type="text/javascript">
    $(document).ready(function(){
        $(".them-gio-hang").click(function(){
            var pID = $(this).attr('rel'); 
            var tenP = $("#ten_sp_"+pID).text();
            // Ajax them vao gio hang san pham tuong ung
            $.post(
            '/ajax/themgiohang.php',
            {
                id:pID,
                ten:tenP
            },
            function(data) {
                alert(data);
            }
        )
        });
    });
</script>

<?php if (isset($_GET['status']) && $_GET['status'] == 1): ?>
    <script type="text/javascript">
        $(document).ready(function(){
            alert('Chúc mừng bạn đặt hàng thành công!!!')
        });
    </script>
<?php endif; ?>
<?php include('layout/footer.php') ?>